#!/usr/bin/env python
#coding:utf-8
"""
提供spider所需的保存页面内容到sqlite数据库
"""
import sqlite3
from  urllib import unquote,quote
class Database(object):
    def __init__(self,dbfile):
        try:
            self.conn = sqlite3.connect(dbfile,isolation_level=None, check_same_thread = False)
            self.conn.execute('''CREATE TABLE IF NOT EXISTS
                                results (id INTEGER PRIMARY KEY AUTOINCREMENT,
                                url TEXT,
                                pagesource TEXT,
                                keywords TEXT)''')
            self.conn.text_factory = str
            self.cur = self.conn.cursor()
        except Exception,e:
            self.conn = None
            self.cur = None


    def close(self):
        if self.conn:
            self.conn.close()


    def save_data(self, url, pagesource, keywords=''):
        try:
            pagesource =  unicode(pagesource,'utf-8')
        except UnicodeDecodeError:
            pagesource =  unicode(pagesource,'gbk')
        if keywords:
            try:
                keywords =  unicode(keywords,'utf-8')
            except UnicodeDecodeError:
                keywords =  unicode(keywords,'gbk')
        url = quote(unquote(url),safe=':?=/')
        if self.conn:
            sql='''INSERT INTO results (url, pagesource, keywords) VALUES (?, ?, ?);'''
            self.conn.execute(sql, (url, pagesource, keywords) )
        else:
            raise sqlite3.OperationalError,'Database connect error'

if __name__ == '__main__':
    import urllib2
    url = 'http://www.56dg.com/'
    html = urllib2.urlopen(url).read()
    # try:
    #     html =  unicode(html,'utf-8')
    # except UnicodeDecodeError:
    #     html =  unicode(html,'gbk')
    Database.save_data(url,html)